
import Vue from 'vue'
import VueRouter from 'vue-router'
import { Toast } from 'vant';
Vue.use(VueRouter)
// const VueRouterPush = VueRouter.prototype.push
// VueRouter.prototype.push = function push (to) {
//   return VueRouterPush.call(this, to).catch(err => err)
// }
// const originalPush = VueRouter.prototype.push
// VueRouter.prototype.push = function push (location, onResolve, onReject) {
//   if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
//   return originalPush.call(this, location).catch(err => err)
// }


const routes = [
  {
    path: '/',
    name: 'home',
    component: () => import(/* webpackChunkName: "home" */ '../views/Home.vue'),
    children: [
      {
        path: '/home/popup',
        name: 'popup',
        component: () => import(/* webpackChunkName: "popup" */ '../views/Popup.vue'),
        meta: {
          keepAlive: true
        }
      }
    ]
  },
  {
    path: '/subject',
    name: 'subject',
    component: () => import('../views/Subject.vue'),
    meta: {
      index: 1
    }
  },
  {
    path: '/classify',
    name: 'classify',
    component: () => import('../views/Classify.vue'),
    meta: {
      index: 1
    },
    children: [
      {
        path: '/classify/popup',
        name: 'cpopup',
        component: () => import('../views/Popup.vue'),
        meta: {
          keepAlive: true
        }
      }
    ]
  },
  // 购物车
  {
    path: "/cart",
    name: 'cart',
    component: () => import('../views/Cart.vue')
  },
  // 我的
  {
    path: "/my",
    name: 'my',
    component: () => import('../views/My.vue')
  },
  // 频道
  {
    path: "/channel",
    name: 'channel',
    component: () => import('../views/Channel'),
    meta: {
      keepAlive: true
    }
  },
  // 品牌
  {
    path: '/brand',
    name: 'brand',
    component: () => import('../views/Brand'),
    meta: {
      keepAlive: true
    }
  },
  // 产品明细
  {
    path: '/details',
    name: 'details',
    component: () => import('../views/GoodsDetail'),
    meta: {
      keepAlive: true
    }
  }
]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})


// router.beforeEach((to, from, next) => {
//   if (localStorage.getItem("token")) {
//     next()
//   } else {
//     if (to.path == "/cart") {
//       Toast.loading({
//         message: '请先登录...',
//         forbidClick: true,
//         loadingType: 'spinner',
//       });

//       setTimeout(() => {
//         next("/my");
        
//       }, 1000);
//       if (localStorage.getItem("token")) {
//         next();
//       } else {
//         return
//       }
//     }
//   }
//   next()
// })


router.beforeEach((to,from,next)=>{
  if (to.path == "/cart") {
    let token = localStorage.getItem("token");
    if(token){
      next()
    }else{
      Toast.loading({
                message: '请先登录...',
                forbidClick: true,
                loadingType: 'spinner',
              });
        
              setTimeout(() => {
                next("/my");
                
              }, 1000);
              // next("/my")
    }
  }else{
    next()
  }
})


export default router
